/**
 * 
 */
package com.taobao.wolfwolf.hibernate.dao.impl;

import java.util.List;

import com.taobao.wolfwolf.hibernate.dao.ShopperDao;
import com.taobao.wolfwolf.hibernate.pojo.Shopper;

public class ShopperDaoImpl extends BaseDaoImpl implements ShopperDao {

	@SuppressWarnings("unchecked")
	public Shopper getLastShopper() {
		List<Shopper> list = getHibernateTemplate().find("from Shopper as t where t.id=(select max(s.id) from Shopper s)");
		return list.size() == 1 ? list.get(0) : null;
	}

	@SuppressWarnings("unchecked")
	public List<Shopper> findAll() {
		return getHibernateTemplate().find("from Shopper");
	}

	@Override
	public long getTotal() throws Exception {
		return this.countByCriteria(Shopper.class);
	}

	@Override
	public String[][] listToArray(List<Shopper> shoppers, int row) {
		String[][] array = new String[row][5];
		
		int i = 0;
		for(Shopper s : shoppers) {
			array[i][0] = s.getName();
			array[i][1] = String.valueOf(s.getCredit());
			array[i][2] = String.valueOf(s.getFavour());
			array[i][3] = String.valueOf(s.getQuantity());
			array[i++][4] = s.getDate();
		}
		return array;
	}

	@Override
	public String[][] loadAll() throws Exception {
		return listToArray(getHibernateTemplate().loadAll(Shopper.class), (int) getTotal());
	}
}
